﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Security.Permissions;
using System.Drawing;

namespace WebSite.CustomControl
{
    /// <summary>
    /// RounTextBox is a custom control which cames from TextBox
    /// </summary>
    [
        AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
        AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
        ToolboxData("<{0}:RoundTextBox runat=\"server\"> </{0}:RoundTextBox>"),
        DefaultProperty("Text"),
    ]
    public class RoundTextBox : TextBox
    {
        private static readonly object EventOnFocusKey = new object();

        public RoundTextBox()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        [
            Bindable(false),
            Category("Appearance"),
            Description("Tips what should be input"),
            DefaultValue("")
        ]
        public string DefaultText
        {
            get
            {
                string s = ViewState["DefaultText"] as string;
                return string.IsNullOrEmpty(s) ? "Please input text!" : s;
            }
            set
            {
                ViewState["DefaultText"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            Height = 20;
            //AddAttributesToRender(writer);

            //writer.RenderBeginTag(HtmlTextWriterTag.Table);
            //writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            //writer.RenderBeginTag(HtmlTextWriterTag.Td);

            writer.AddStyleAttribute(HtmlTextWriterStyle.OverflowY, "1");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "1");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Src, "images/textbox_l.png");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();  //close img left

            writer.AddStyleAttribute(HtmlTextWriterStyle.Color, "gray");
            writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "top");
            writer.AddStyleAttribute(HtmlTextWriterStyle.FontStyle, "italic");
            writer.AddStyleAttribute("width", Width.ToString());
            writer.AddStyleAttribute("height", Height.ToString());
            writer.AddStyleAttribute("border-left-width", "0");
            writer.AddStyleAttribute("border-right-width", "0");
            writer.AddAttribute("onfocus", "this.value=''");
            writer.AddAttribute(HtmlTextWriterAttribute.Value, DefaultText);
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();  //colse input

            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "1");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
            writer.AddAttribute(HtmlTextWriterAttribute.Src, "images/textbox_r.png");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();  //colse img rigth

            //writer.RenderEndTag();  //close td
            //writer.RenderEndTag();  //close tr
            //writer.RenderEndTag();  //close table
        }
    }
}
